package com.uxwine.view.t;

import android.Manifest.permission;
import android.util.Log;
import android.view.View;

public abstract class DialViewAdapter{
	
	private DialView mHostView;
	private float mfBearing;
	private int mBtnResId;
	
	public int getButtonId()
	{
		return mBtnResId;
	}
	
	public void setButtonId(int btnResId)
	{
		mBtnResId = btnResId;
	}
	
	public final float getBearing()
	{
		return mfBearing;
	}
	
	public final void setBearing(float bearing)
	{
		mfBearing = bearing;
	}
	
	public void setPosition(int position)
	{
		if(position < 0 || position >= getCount())
			return;
		float degree = 360.0f / (float)getCount(); 
		float dutamDegree = ((float)position) * degree;
		setBearing(dutamDegree);	
	}
	
	public final void setHostView(DialView view)
	{
		mHostView = view;
	}
	
	public final void notifyDataChanged()
	{
		if(mHostView != null)
			mHostView.updateView();
	}
		
	public abstract int getCount();	
	public abstract String getMarkerText(int position);
	public abstract long getValue(int position);
	
	public final float getNearBearing(float bearing)
	{
		float degree = 360.0f / (float)getCount(); 
		return degree * getPosition(bearing);
	}
	
	public final boolean isMarked(int position)
	{
		float degree = 360.0f / (float)getCount(); 
		float dutamDegree = ((float)position) * degree;
		return (dutamDegree - degree / 2 <= mfBearing && mfBearing < dutamDegree + degree / 2);
	}
	
	public final int getPosition()
	{
		int p =  getPosition(mfBearing);

		Log.d("uxwine", "br:" + mfBearing + ", pn:" + p);
		
		return p;
	}
	
	public int getPosition(float bearing)
	{
		float degree = 360.0f / (float)getCount(); 
		int position = (int)(bearing / degree);
		float offset = bearing % degree;
		if(bearing > 360.0f - degree / 2)
		{
			return getCount() - 1;
		}
		if(offset > degree / 2)
			return position + 1;
		else 
			return position;
	}
}
